<?php

namespace App\Http\Controllers\Mobile\Company;
use App\Models\Company\ResumeFavorite;
use App\Models\Personal\Interview;
use App\Models\Personal\ResumeSend;
use Illuminate\Http\Request;
use App\Models\Common\CollArea;
use App\Models\Common\CollOption;
use App\Facades\Style;
use Illuminate\Support\Facades\DB;

class InterviewController extends CommonController{
    /**
     * 获取面试管理
     * @param Request $request
     * @return mixed
     */
    public function message(Request $request){
        $user = auth()->guard('company')->user();
        $relist = DB::table('interview as log')
            ->join('resume as t1', 'log.resume_id', '=', 't1.id')
            ->join('company_job as t2', 'log.job_id', '=', 't2.id')
            ->select(
                'log.id','log.status','log.updated_at','t1.name','t1.sex','t1.birthday',
                't1.education','t1.homeaddress','t2.job_title','log.job_id','log.resume_id'
            )
            ->where('log.company_uid',$user->uid)
            ->where(function ($query) use ($request) {
                if(strlen($request->keyWord)>0){
                    $query->where('t2.job_title', 'like', '%'.$request->keyWord.'%')->orWhere('t1.name','like','%'.$request->keyWord.'%');
                }
            })
            ->where(function ($query) use ($request) {
                if(strlen($request->status)>0){
                    $query->where('log.status', $request->status);
                }
            })
            ->where('log.status','!=',4)
            ->orderBy('updated_at','DESC')
            ->paginate(20);
        foreach($relist as $v){
            switch($v->sex){
                case "1":
                    $v->cnsex='男';
                    break;
                case "2":
                    $v->cnsex='女';
                    break;
                default:
                    $v->cnsex='';
            }
            $v->education=CollOption::getOptionName($v->education,'education');
            $v->homeaddress=CollArea::getAreaName($v->homeaddress);
            $v->birthday=birthday($v->birthday);
        }
        $status=$request->status;
        return Style::view('mobile.company.interviewManage',compact('relist','status'));
    }
    /**
     * 放入人才库
     * @param Request $request
     */
    public function toFavorite(Request $request){
        $user = auth()->guard('company')->user();
        $interview=Interview::select('resume_id','job_id')->where('company_uid',$user->uid)->where('id',$request->id)->first();
        $favorite=ResumeFavorite::select('id')->where('uid',$user->uid)->where('status','!=',4)->where("resume_id",$interview->resume_id)->first();
        if(!empty($favorite->id)){
            $favorite->updated_at=date('Y-m-d H:i:s');
            $favorite->status = 0;
            $favorite->save();
        }else{
            $favorite = new ResumeFavorite;
            $favorite->uid = $user->uid;
            $favorite->resume_id = $interview->resume_id;
            $favorite->job_id = $interview->job_id;
            $favorite->status = 0;
            $favorite->save();
        }
        return response()->json(['status' => 200,'message'=>"已将简历加入人才库"]);
    }
}